<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FangOwnerExport;
use App\Models\FangOwner;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;


class FangOwnerController extends BaseController
{
    //房东列表
    public function index()
    {
        $data = FangOwner::all();
        return view('admin.fangowner.index',compact('data'));
    }

    //导出excel
    public function exports(){
        return Excel::download(new FangOwnerExport(),'fangdong.xlsx');
    }

    //房东添加 显示
    public function create()
    {
        return view('admin.fangowner.create');
    }

    //文件上传
    public function upfile (Request $request){
        $pic = config('up.pic');
        if($request->hasFile('file')){
            $ret = $request->file('file')->store('','fangowner');
            $pic = '/uploads/fangowner/'.$ret;
        }
        return ['status'=>0,'url'=> $pic];
    }
    //图片删除
    public function delfile(Request $request){
        $filepath = $request->get('file');
        //得到真实地址
        $path = public_path().$filepath;
        //删除指定文件
        unlink($path);
        return['status'=>0,'msg'=>'成功'];
    }
    //房东添加 处理
    public function store(Request $request)
    {
        $this->validate($request,[
            'name' => 'required',
            'phone'=>'required'
        ]);
        //获取数据
        $postData = $request->except(['_token','file']);
        //去除第一个#
        $postData['pic'] = trim($postData['pic'],'#');
        //入库
        FangOwner::create($postData);
        return ['status' => 0,'msg' => '添加房东成功'];
    }

    //查看身份证图片
    public function show(FangOwner $fangowner)
    {
        //获取数据已#号为分隔符分割成数组
        $picList = explode('#',$fangowner->pic);
        array_map(function($item){
            //输出数组中每一个地址
            echo "<div><img src=$item style='width:150px;'/></div>";
        },$picList);
    }

    //修改房东 显示
    public function edit(FangOwner $fangowner)
    {
        //
    }

    //修改房东 处理
    public function update(Request $request, FangOwner $fangowner)
    {
        //
    }

    //删除房东
    public function destroy(FangOwner $fangowner)
    {
        //
    }
}
